﻿using UnityEngine;
using System.Collections;

public class ScreenControlViewModel
{

    #region Variables

    private float _speedMove = 1f;
	private float _speedRotation = 1f;
	private GameObject _mainCharacther;
	private Camera _mainCamera;
    private float _minAngleCam;
    private float _maxAngleCam;
    private float _distanceH;
    private float _distanceW;
	public GameObject MainCharacther
	{
		get { return _mainCharacther; }
		set {_mainCharacther = value; }
	}
	public Camera MainCamera
	{
		get { return _mainCamera; }
		set { _mainCamera = value; } 
	}
	public float SpeedMove
	{
		get{ return _speedMove; }
		set { _speedMove = value; }
	}
	public float SpeedRotation
	{
		get{ return _speedRotation; }
		set { _speedRotation = value; }
	}
    public float MinAngleCam
    {
        get { return _minAngleCam; }
        set { _minAngleCam = value; }
    }
    public float MaxAngleCam
    {
        get { return _maxAngleCam; }
        set { _maxAngleCam = value; }
    }
    public float CamDistanceH
    {
        get { return _distanceH; }
        set { _distanceH = value; }
    }
    public float CamDistanceW
    {
        get { return _distanceW; }
        set { _distanceW = value; }
    }
    private CollisionFlags collisionFlags;
    private CharacterController cCharacther;
    private BoxCollider bxCollider;
    private Quaternion rot;
    private Quaternion toTarget;
    private Quaternion rotation;
    private Quaternion rotationC;
    private Vector3 euler;
    private Vector3 dir;
    private Vector3 nextPosition;
    private Vector3 position;
    private Vector3 Def = Vector3.zero;
    private Vector3 tes; 
    private float x = 0.0f;
    private float y = 0.0f;
    #endregion

    public ScreenControlViewModel(GameObject character, Camera camera, CharacterController cCharacther,BoxCollider bxCollider)
	{
		MainCharacther = character;
		MainCamera = camera;
        this.cCharacther = cCharacther;
        this.bxCollider = bxCollider;
	}
    
    public void MoveObject(UIJoystick touch)
    {
        x += touch.joystickPosition.x * SpeedRotation * 0.8f;
        y += touch.joystickPosition.y * SpeedRotation * -0.8f;
        y = (y > MaxAngleCam) ? MaxAngleCam : y;
        y = (y < MinAngleCam) ? MinAngleCam : y;
        
        //Character
        rotationC = Quaternion.Euler(0, x, 0);
        if (touch.joystickPosition.y != 0)
        {
            Def.z = SpeedMove;
            Def.y = 0.0f;
            dir = rotationC * Def;
            
            cCharacther.SimpleMove(dir);
            
            //tes = new Vector3(cCharacther.transform.position.x, 2.7f, cCharacther.transform.position.z);
            bxCollider.transform.position = cCharacther.transform.position; // tes;
        }
        cCharacther.transform.rotation = rotationC;
        
        //Camera
        rotation = Quaternion.Euler(y, x, 0);
        Def.y = CamDistanceH;
        Def.z = -CamDistanceW;
        position = rotation * Def + cCharacther.transform.position;
        MainCamera.transform.rotation = rotation;
        MainCamera.transform.position = position;

    }

    #region Camera Settings

    /// <summary>
    /// Set the initial Configuration for te Camera
    /// </summary>
    public void StartCamera()
    {
        Vector3 startPos = new Vector3(155, 20, -130);
        MainCamera.transform.position = cCharacther.transform.TransformDirection(startPos);
        MainCamera.transform.eulerAngles = new Vector3(MinAngleCam, 0, 0);
        Vector3 angles = MainCamera.transform.eulerAngles;
        x = angles.y;
        y = angles.x;
    }


    public void SetCameraProperties(float MinAngleCam,float MaxAngleCam,float CamDistanceW,float CamDistanceH)
    {
        this.MinAngleCam = MinAngleCam;
        this.MaxAngleCam = MaxAngleCam;
        this.CamDistanceW = CamDistanceW;
        this.CamDistanceH = CamDistanceH;
    }
    #endregion

}
